package com.hdustea.base.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;

import com.hdustea.base.util.DimenUtil;


/**
 * @author Weimore
 *         2018/4/3.
 *         description:
 */

public class HorizontalSlideLayout extends HorizontalScrollView {

    private Context mContext;

    private View mLeftView;
    private View mRightView;

    private boolean once;

    private boolean mIsOpen = false;
    private int mRightWidth;

    private toggleChangeListener mListener;

    public HorizontalSlideLayout(Context context) {
        this(context,0);
    }

    public HorizontalSlideLayout(Context context, int rightWidth) {
        this(context, null);
        if(rightWidth<=0){
            mRightWidth =  DimenUtil.dp2px(60);
        }else {
            mRightWidth = DimenUtil.dp2px(rightWidth);
        }

    }

    public HorizontalSlideLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HorizontalSlideLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        setOverScrollMode(OVER_SCROLL_NEVER);
//        LayoutInflater.from(mContext).inflate(R.layout.layout_horizontal_slide, this);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (!once) {
            once = true;
            mRightView.setTranslationX(mIsOpen ? 0 : -mRightView.getWidth());
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mLeftView = ((ViewGroup) getChildAt(0)).getChildAt(0);
        mLeftView.getLayoutParams().width = DimenUtil.getScreenWidth();
        mRightView = ((ViewGroup) getChildAt(0)).getChildAt(1);
//        mRightView.getLayoutParams().width = mRightWidth;
        mRightWidth = mRightView.getLayoutParams().width;
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        mRightView.setTranslationX(getScrollX() - mRightView.getWidth());
//        Log.e("test", "scrollX:" + getScrollX());
//        Log.e("test", "width:" + mRightView.getWidth());
//        Log.e("test", "l:" + l + ", oldl:" + oldl);
    }

    public void setToggleChangeListener(toggleChangeListener listener){
        mListener = listener;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_UP:
                if (getScrollX() < mRightView.getWidth() / 2) {
                    smoothScrollTo(0, 0);
                    mIsOpen = false;
                } else {
                    smoothScrollTo(mRightView.getWidth(), 0);
                    mIsOpen = true;
                }
                if(mListener!=null){
                    mListener.toggleChanged(mIsOpen);
                }
                return true;
            default:
                break;
        }
        return super.onTouchEvent(ev);
    }

    public void toggle(boolean toggle) {
        smoothScrollTo(toggle ? mRightView.getWidth() : 0, 0);
        mIsOpen = toggle;
    }

    public boolean isOpen() {
        return mIsOpen;
    }

    public void toggle() {
        mIsOpen = !mIsOpen;
        smoothScrollTo(mIsOpen ? mRightView.getWidth() : 0, 0);
    }

    public interface toggleChangeListener{
        void toggleChanged(boolean isOpen);
    }

}
